<?php
define('APPMESSAGE','1.00');
/************************************************************************/
/* FrameWork  test preview                                              */
/* ==================================================================== */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/

if (!file_exists("datas/$path/msgusers"))mkdir("datas/$path/msgusers",0755);

autostyle("$path/$appname");

$user=user_getuser();

if ($user==""){
    echo("riservato agli utenti iscritti");
    httplog();
    die();
}

//carica la lingua se esiste
$lang=lang();
if (file_exists("$path/$appname/lang/$lang.inc"))
    include "$path/$appname/lang/$lang.inc";
else
    include "$path/$appname/lang/it.inc";

?>

<?php structure_openBlock("table appl",apptitle($appname),""); ?>


	<p>

<?php

	$op=$_GET['op'];

	$listuser=user_listusers();

	$listgroups=group_listgroups();

	echo "<a href=\"index.php?appname=$appname\"><img border=0 src=\"applications/$appname/images/home.png\" alt=\""._MSGINBOX."\" /></a>\n";

	echo "<a href=\"index.php?appname=$appname&amp;op=sent_messages\"><img border=0 src=\"applications/$appname/images/sent.gif\" alt=\""._MSGSENT."\" /></a>\n";

	echo "<a href=\"index.php?appname=$appname&amp;op=write_message\"><img border=0 src=\"applications/$appname/images/write.png\" alt=\""._MSGNEW."\" /></a>\n";

	echo "<a href=\"index.php?user_op=logout\"><img border=0 src=\"applications/$appname/images/logout.png\" alt=\""._LOGOUT."\" /></a>\n";

	echo "<br />\n";

	switch($op) {

	default:

		$dir = "datas/$path/msgusers/$user";

		if ($user!="") {
		    if (!file_exists($dir)) mkdir($dir,0755);
		    if (!file_exists("$dir/received")) mkdir("$dir/received",0755);
		    if (!file_exists("$dir/received-tmp")) mkdir("$dir/received-tmp",0755);
		    if (!file_exists("$dir/sent")) mkdir("$dir/sent",0755);
		}

	//conta i messaggi ricevuti

		$msgcount = messages_count("$dir/received");
		$msgcount2 = messages_count("$dir/received-tmp");
		echo "<h2 class=\"inbox\">"._MSGINBOX.": ".$msgcount." - "._MSGNOTREAD.": ".$msgcount2."</h2>\n";

		$listmsg = messages_list("$dir/received");

		if (count($listmsg)>0) {

		echo "<form method=\"post\" action=\"index.php?appname=$appname&amp;op=delete_message&amp;loc=received\">";
		echo "<table class=\"messagelist\">";
		echo "	<tr class='firstrow'>
					<td></td>
					<td style=\"width: 15%\">"._MSGFROM."</td>
					<td style=\"width: 55%\">"._MSGSUBJECT."</td>
					<td style=\"width: 25%\">"._MSGDATA."</td>
				</tr>\n";

			foreach($listmsg as $msg) {

				$xml = messages_read("$dir/received",$msg);

				echo "<tr class=\"".messages_isred($dir,$msg)."\">\n";
				echo "\t<td><input type=\"checkbox\" name=\"".substr($msg,0,-4)."\" value=\"checked\" /><img src=\"applications/$appname/images/".messages_isred($dir,$msg).".gif\" /></td>";
				echo "\t<td><a href=\"index.php?appname=_reguser&op=view&user=".$xml['from']."\">".$xml['from']."</a></td>\n";
				echo "\t<td><a href=\"index.php?appname=$appname&amp;op=read_message&amp;loc=received&amp;id=$msg\">".$xml["title"]."</a></td>\n";
				echo "\t<td>".date("d M Y - H:i",filemtime("$dir/received/$msg"))."</td>\n";
				echo "<tr>\n";
			}

		echo "</table>\n";
		echo "<div class=\"buttons\">\n";
		echo "\t<button type=\"submit\">"._MSGDELETE."</button>\n";
		echo "</div>";
		echo "</form>";

		}

	break;

	case "sent_messages":

		$dir = "datas/$path/msgusers/$user/sent";
		$msgcount = messages_count($dir);
		print "<h2 class=\"inbox\">Inviati: ".$msgcount."</h2>\n";

		echo "<form method=\"post\" action=\"index.php?appname=$appname&amp;op=delete_message&amp;loc=sent\">";
		echo "<table class=\"messagelist\">";
		echo "	<tr class='firstrow'>
					<td></td>
					<td style=\"width: 20%\">"._MSGTO."</td>
					<td style=\"width: 40%\">"._MSGSUBJECT."</td>
					<td style=\"width: 35%\">"._MSGDATA."</td>
				</tr>\n";

		$listmsg = messages_list($dir);
		if (count($listmsg)>0) {
			foreach($listmsg as $msg) {

				$xml = messages_read($dir,$msg);

				echo "<tr>\n";
				echo "\t<td><input type=\"checkbox\" name=\"".substr($msg,0,-4)."\" value=\"checked\" /></td>";
				echo "\t<td><a href=\"index.php?appname=reguser&op=view&user=".$xml['user']."\">".$xml['user']."</a></td>\n";
				echo "\t<td><a href=\"index.php?appname=$appname&amp;op=read_message&amp;loc=sent&amp;id=$msg\">".$xml["title"]."</a></td>\n";
				echo "\t<td>".date("d M Y - H:i",filemtime("$dir/$msg"))."</td>\n";
				echo "<tr>\n";
			}
		}

		echo "</table>";
		echo "<div class=\"buttons\">\n";
		echo "\t<button type=\"submit\">"._MSGDELETE."</button>\n";
		echo "</div>";
		echo "</form>";

	break;

	case "write_message": ?>

		<form name="send_msg" method="post" action="index.php?appname=<?=$appname?>&amp;op=send_message">

			<?=_MSGFROM;?>:<input name="from" type="text" value="<?=$user?>" readonly="yes" />

			<?=_MSGTO;?>:<select name="to">
				<?php foreach($listuser as $user_name)echo "<option>".$user_name."</option>\n"; ?>
			</select><br />

			<?=_MSGSUBJECT;?>:<br /><input name="sobject" type="text" style="width:100%;" /><br />

			<?=_MSGCONTENT;?>:<br /><textarea name="body" style="width:100%; height:100;" ></textarea><br />

			<div class="buttons">
				<button type="submit"><?=_MSGSEND;?></button>
			</div>

		</form>

	<?php
	break;

	case "reply_message":

		$dir = $_GET['dir'];
		$msg = $_GET['msg'];

		$xml = messages_read($dir,$msg); ?>

		<form name="send_msg" method="post" action="index.php?appname=<?=$appname?>&amp;op=send_message">

			<?=_MSGFROM;?>:<input name="from" type="text" value="<?=$user?>" readonly="yes" />

			<?=_MSGTO;?>:<input name="to" type="text" value="<?=$xml['from']?>" readonly="yes" /><br />

			<?=_MSGSUBJECT;?>:<input name="sobject" type="text" style="width:100%;" value="<?=$xml['title'];?>" /><br />

			<?=_MSGCONTENT;?>:<textarea name="body" style="width:100%; height:100;" ></textarea><br />

			<div class="buttons">
				<button type="submit"><?=_MSGSEND;?></button>
			</div>

		</form>

	<?php
	break;

	case "send_message": ?>

	<?php

		$msgdir = "datas/$path/msgusers";
		$msgfrom = $_POST['from'];
		$msgto = $_POST['to'];
		$msgsobject = $_POST['sobject'];
		$msgbody = $_POST['body'];

		messages_send($msgdir,$msgfrom,$msgto,$msgsobject,$msgbody);

		echo "<h2 class=\"inbox\">Il messaggio &egrave; stato inviato correttamente</h2>";

	break;

	case "read_message":

		$loc = $_GET['loc'];
		$file = $_GET['id'];
		$dir = "datas/$path/msgusers/$user";

		$xml = messages_read("$dir/$loc",$file);

		?>

		<h4><?=$xml['from']?> - <?=$xml['title']?></h4>

		<p><?=$xml['body']?></p>

		<br />

		<?php

		echo "<a href=\"index.php?appname=$appname&amp;op=reply_message&amp;dir=$dir/$loc&amp;msg=$file\">Reply</a>";

		if(file_exists("$dir/received-tmp/$file.tmp")) unlink("$dir/received-tmp/$file.tmp");

	break;

	case "delete_message":

		$dir = "datas/$path/msgusers/$user";
		$loc = $_GET['loc'];

		$listmsg = messages_list("$dir/$loc");

		foreach($listmsg as $msg) {
			$del = substr($msg,0,-4);
			if ($_POST["$del"] == "checked") {
				messages_delete("$dir/$loc/$msg");
				if (file_exists("$dir/$loc-tmp/$msg.tmp")) messages_delete("$dir/$loc-tmp/$msg.tmp");
			}
		}

	break;

	}

?>

	</p>

<?php structure_closeBlock(); ?>
